package org.jtao.gbsubject.web.rest;

import com.codahale.metrics.annotation.Timed;
import org.jtao.gbsubject.service.CurrentStateService;
import org.jtao.gbsubject.web.rest.errors.BadRequestAlertException;
import org.jtao.gbsubject.web.rest.util.HeaderUtil;
import org.jtao.gbsubject.web.rest.util.PaginationUtil;
import org.jtao.gbsubject.service.dto.CurrentStateDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing CurrentState.
 */
@RestController
@RequestMapping("/api")
public class CurrentStateResource {

    private final Logger log = LoggerFactory.getLogger(CurrentStateResource.class);

    private static final String ENTITY_NAME = "currentState";

    private final CurrentStateService currentStateService;

    public CurrentStateResource(CurrentStateService currentStateService) {
        this.currentStateService = currentStateService;
    }

    /**
     * POST  /current-states : Create a new currentState.
     *
     * @param currentStateDTO the currentStateDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new currentStateDTO, or with status 400 (Bad Request) if the currentState has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/current-states")
    @Timed
    public ResponseEntity<CurrentStateDTO> createCurrentState(@RequestBody CurrentStateDTO currentStateDTO) throws URISyntaxException {
        log.debug("REST request to save CurrentState : {}", currentStateDTO);
        if (currentStateDTO.getId() != null) {
            throw new BadRequestAlertException("A new currentState cannot already have an ID", ENTITY_NAME, "idexists");
        }
        CurrentStateDTO result = currentStateService.save(currentStateDTO);
        return ResponseEntity.created(new URI("/api/current-states/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    @PostMapping("/current-states/import")
    @Timed
    public ResponseEntity<Integer> importCurrentStates(@RequestParam("file") MultipartFile file) {
        log.debug("REST request to import CurrentStates : {} ",file.getOriginalFilename());
        Integer sum=currentStateService.importCurrentStates(file);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityImportAlert(ENTITY_NAME,sum.toString()))
            .body(sum);
    }

    /**
     * PUT  /current-states : Updates an existing currentState.
     *
     * @param currentStateDTO the currentStateDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated currentStateDTO,
     * or with status 400 (Bad Request) if the currentStateDTO is not valid,
     * or with status 500 (Internal Server Error) if the currentStateDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/current-states")
    @Timed
    public ResponseEntity<CurrentStateDTO> updateCurrentState(@RequestBody CurrentStateDTO currentStateDTO) throws URISyntaxException {
        log.debug("REST request to update CurrentState : {}", currentStateDTO);
        if (currentStateDTO.getId() == null) {
            return createCurrentState(currentStateDTO);
        }
        CurrentStateDTO result = currentStateService.save(currentStateDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, currentStateDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /current-states : get all the currentStates.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of currentStates in body
     */
    @GetMapping("/current-states")
    @Timed
    public ResponseEntity<List<CurrentStateDTO>> getAllCurrentStates(Pageable pageable) {
        log.debug("REST request to get a page of CurrentStates");
        Page<CurrentStateDTO> page = currentStateService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/current-states");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    @GetMapping("/current-statesBySubjectId/{id}")
    public ResponseEntity<List<CurrentStateDTO>> getAllCurrentStatesBySubjectId(@PathVariable Long id){
        log.debug("REST request to get CurrentStates by SubjectId : {}",id);
        Page<CurrentStateDTO> page = currentStateService.findAllBySubjectId(id);
        HttpHeaders headers =PaginationUtil.generatePaginationHttpHeaders(page,"/api/current-statesBySubjectId/{id}");
        return new ResponseEntity<>(page.getContent(),headers,HttpStatus.OK);
    }

    /**
     * GET  /current-states/:id : get the "id" currentState.
     *
     * @param id the id of the currentStateDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the currentStateDTO, or with status 404 (Not Found)
     */
    @GetMapping("/current-states/{id}")
    @Timed
    public ResponseEntity<CurrentStateDTO> getCurrentState(@PathVariable Long id) {
        log.debug("REST request to get CurrentState : {}", id);
        CurrentStateDTO currentStateDTO = currentStateService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(currentStateDTO));
    }

    /**
     * DELETE  /current-states/:id : delete the "id" currentState.
     *
     * @param id the id of the currentStateDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/current-states/{id}")
    @Timed
    public ResponseEntity<Void> deleteCurrentState(@PathVariable Long id) {
        log.debug("REST request to delete CurrentState : {}", id);
        currentStateService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
